package com.gexiaobao.pigeon.ui.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatRadioButton
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.widget.addTextChangedListener
import com.gexiaobao.pigeon.R
import me.hgj.jetpackmvvm.base.appContext

/**
 * @Author      : hxw
 * @Date        : 2024/6/18 8:44
 * @Describe    :
 */
class RxDialogAddPigeon(context: Context?) : RxDialog(context!!) {

    private var pigeonSexId = 0
    private var pigeonEyePattern = 0

    private lateinit var tvDialogRingIdYear: AppCompatEditText
    private lateinit var tvDialogRingIdArea: AppCompatEditText
    private lateinit var tvDialogRingId: AppCompatEditText
    private lateinit var etDialogName: AppCompatEditText
    private lateinit var dialogRbAddPigeonSexNo: AppCompatRadioButton
    private lateinit var dialogRbAddPigeonSexMale: AppCompatRadioButton
    private lateinit var dialogRbAddPigeonSexFemale: AppCompatRadioButton
    private lateinit var tvDialogFeatherColor: AppCompatTextView
    private lateinit var ivDialogFeatherColor: AppCompatImageView
    private lateinit var rbDialogEyePatternUnKnow: AppCompatRadioButton
    private lateinit var rbDialogEyePatternYellow: AppCompatRadioButton
    private lateinit var rbDialogEyePatternSha: AppCompatRadioButton
    private lateinit var rbDialogEyePatternNiu: AppCompatRadioButton
    private lateinit var etDialogPigeonDescribe: AppCompatEditText
    private lateinit var tvSure: AppCompatTextView
    private lateinit var tvCancel: AppCompatTextView
    private lateinit var tvDialogMember: AppCompatTextView
    private lateinit var ivDialogMember: AppCompatImageView

    init {
        initView()
    }


    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_delivering_pigeon, null)
        tvDialogRingIdYear = dialogView.findViewById(R.id.tv_ring_number_year)
        tvDialogRingIdArea = dialogView.findViewById(R.id.tv_ring_number_area)
        tvDialogRingId = dialogView.findViewById(R.id.tv_ring_number)
        etDialogName = dialogView.findViewById(R.id.etDialogName)
        dialogRbAddPigeonSexNo = dialogView.findViewById(R.id.dialogRbAddPigeonSexNo)
        dialogRbAddPigeonSexMale = dialogView.findViewById(R.id.dialogRbAddPigeonSexMale)
        dialogRbAddPigeonSexFemale = dialogView.findViewById(R.id.dialogRbAddPigeonSexFemale)
        tvDialogFeatherColor = dialogView.findViewById(R.id.tvDialogFeatherColor)
        ivDialogFeatherColor = dialogView.findViewById(R.id.ivDialogFeatherColor)
        rbDialogEyePatternUnKnow = dialogView.findViewById(R.id.rbDialogEyePatternUnKnow)
        rbDialogEyePatternYellow = dialogView.findViewById(R.id.rbDialogEyePatternYellow)
        rbDialogEyePatternSha = dialogView.findViewById(R.id.rbDialogEyePatternSha)
        rbDialogEyePatternNiu = dialogView.findViewById(R.id.rbDialogEyePatternNiu)
        etDialogPigeonDescribe = dialogView.findViewById(R.id.etDialogPigeonDescribe)
        tvSure = dialogView.findViewById(R.id.tvSure)
        tvCancel = dialogView.findViewById(R.id.tvCancel)
        tvDialogMember = dialogView.findViewById(R.id.tvDialogMember)
        ivDialogMember = dialogView.findViewById(R.id.ivDialogMember)

        tvCancel.setOnClickListener {
            dismiss()
        }

        checkedListeners()
        addTextWatch()
        setContentView(dialogView)
    }

    fun setCommitListener(listener: View.OnClickListener?) {
        tvSure.setOnClickListener(listener)
    }

    fun setFeatherColorListener(listener: View.OnClickListener?) {
        tvDialogFeatherColor.setOnClickListener(listener)
        ivDialogFeatherColor.setOnClickListener(listener)
    }

    fun setMemberListener(listener: View.OnClickListener?) {
        tvDialogMember.setOnClickListener(listener)
        ivDialogMember.setOnClickListener(listener)
    }

    fun setFeatherColor(color: String) {
        tvDialogFeatherColor.text = color
    }

    fun setMemberName(member: String) {
        tvDialogMember.text = member
    }

    fun getPigeonName(): String {
        return etDialogName.text.toString().trim()
    }

    fun getPigeonSex(): Int {
        return pigeonSexId
    }

    fun getPigeonSexStr(): String {
        return if (dialogRbAddPigeonSexNo.isChecked) {
            context.getString(R.string.hint_female)
        } else if (dialogRbAddPigeonSexMale.isChecked) {
            context.getString(R.string.hint_male)
        } else {
            context.getString(R.string.hint_female)
        }
    }

    fun getPigeonEyePatternStr(): String {
        return if (rbDialogEyePatternUnKnow.isChecked) {
            appContext.getString(R.string.hint_unknown)
        } else if (rbDialogEyePatternYellow.isChecked) {
            appContext.getString(R.string.pigeon_yellow)
        } else if (rbDialogEyePatternSha.isChecked) {
            appContext.getString(R.string.pigeon_sha)
        } else {
            appContext.getString(R.string.pigeon_bull)
        }
    }

    fun getFeatherColorStr(): String {
        return tvDialogFeatherColor.text.toString().trim()
    }

    fun getEyePattern(): Int {
        return pigeonEyePattern
    }

    fun getRingIdYear(): String {
        return tvDialogRingIdYear.text.toString().trim()
    }

    fun getRingIdArea(): String {
        return tvDialogRingIdArea.text.toString().trim()
    }

    fun getRingId(): String {
        return tvDialogRingId.text.toString().trim()
    }

    fun getPigeonDescribe(): String {
        return etDialogPigeonDescribe.text.toString().trim()
    }

    fun getFeatherColor(): String {
        return tvDialogFeatherColor.text.toString().trim()
    }

    private fun checkedListeners() {
        dialogRbAddPigeonSexNo.setOnCheckedChangeListener { _, b ->
            if (b) pigeonSexId = 1
        }

        dialogRbAddPigeonSexMale.setOnCheckedChangeListener { _, b ->
            if (b) pigeonSexId = 3

        }
        dialogRbAddPigeonSexFemale.setOnCheckedChangeListener { _, b ->
            if (b) pigeonSexId = 2
        }

        rbDialogEyePatternUnKnow.setOnCheckedChangeListener { _, b ->
            if (b) pigeonEyePattern = 1
        }

        rbDialogEyePatternYellow.setOnCheckedChangeListener { _, b ->
            if (b) pigeonEyePattern = 2
        }

        rbDialogEyePatternSha.setOnCheckedChangeListener { _, b ->
            if (b) pigeonEyePattern = 3
        }

        rbDialogEyePatternNiu.setOnCheckedChangeListener { _, b ->
            if (b) pigeonEyePattern = 4
        }
    }

    private fun addTextWatch() {
        tvDialogRingIdYear.addTextChangedListener {
            if (it?.length == 4) {
                tvDialogRingIdArea.requestFocus()
            }
        }

        tvDialogRingIdArea.addTextChangedListener {
            if (it?.length == 2) {
                tvDialogRingId.requestFocus()
            }
        }
    }

}